package longrise.phone.com.bjjt_jyb.bindinginformation;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.util.Util;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.ApiImpI;
import longrise.phone.com.bjjt_jyb.Utils.CommontUtils;
import longrise.phone.com.bjjt_jyb.Utils.DialogUtils;
import longrise.phone.com.bjjt_jyb.Utils.EnCryptDeCrypt;
import longrise.phone.com.bjjt_jyb.Utils.PrefUtils;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;

/**
 * Created by Administrator on 2016/5/17.
 */
public class ModifyPwdView extends LFView implements View.OnClickListener {

    private Context mContext;

    private View mView;

    private EditText oldPwdET;

    private EditText newPwdET;

    private Button confirmBtn;

    private BaseApplication application;
    private ImageView iv_1;
    private ImageView iv_2;
    private LinearLayout ll_2;
    private LinearLayout ll_1;

    public ModifyPwdView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        if (null == mContext) {
            return;
        }
        mView = View.inflate(mContext, R.layout.view_modifypwd, null);
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText(R.string.modify_pwd);
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Util.hideSoftInput(mContext);
                OnDestroy();
                closeForm();
            }
        });
        oldPwdET = (EditText) mView.findViewById(R.id.mpwd_et_oldpwd);
        newPwdET = (EditText) mView.findViewById(R.id.mpwd_et_newpwd);
        confirmBtn = (Button) mView.findViewById(R.id.mpwd_btn_confirm);
        ll_1 = (LinearLayout) mView.findViewById(R.id.ll_1);
        ll_2 = (LinearLayout) mView.findViewById(R.id.ll_2);
        iv_1 = (ImageView) mView.findViewById(R.id.iv_1);
        iv_2 = (ImageView) mView.findViewById(R.id.iv_2);

        setListener(true);
        application = (BaseApplication) mContext.getApplicationContext();
    }

    private void setListener(boolean flag) {
        if (confirmBtn != null) {
            confirmBtn.setOnClickListener(flag ? this : null);
        }
        if (ll_1 != null) {
            ll_1.setOnClickListener(flag ? this : null);
        }
        if (ll_2 != null) {
            ll_2.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
                case R.id.mpwd_btn_confirm://修改密码
                    modifyPwd();
                    break;
                case R.id.ll_1:
                    CommontUtils.setSeePwd(oldPwdET, iv_1);
                    break;
                case R.id.ll_2:
                    CommontUtils.setSeePwd(newPwdET, iv_2);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改密码
     *
     * @throws Exception
     */
    private void modifyPwd() throws Exception {
        String oldPwd = oldPwdET.getText().toString().trim();
        if (TextUtils.isEmpty(oldPwd)) {
            UiUtil.showToast(mContext, "请输入正确的当前密码");
            return;
        }
        String saveOld = PrefUtils.getString(mContext, "loginPassword", null);
        if (!TextUtils.equals(oldPwd, saveOld)) {
            UiUtil.showToast(mContext, "当前密码输入不正确");
            return;
        }
        final String newPwd = newPwdET.getText().toString().trim();
        if (TextUtils.isEmpty(newPwd)) {
            UiUtil.showToast(mContext, "请输入正确的新密码");
            return;
        }
        String userflag = application.getKckpName();
        String oldPwdEn = application.getKckpPass();
        String token = application.getToken();
        final String newPwdEn = EnCryptDeCrypt.getInstance().EnCrypt(newPwd);
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(application).modifyPwd(userflag, oldPwdEn, newPwdEn, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(getContext(), error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean bean = (EntityBean) result;
                        String redes = bean.getString("redes");
                        if (!TextUtils.isEmpty(redes)) {
                            UiUtil.showToast(getContext(), redes);
                        }
                        String restate = bean.getString("restate");
                        if (TextUtils.equals("1", restate)) {
                            //重新设置密码的值
                            PrefUtils.setString(mContext, "loginPassword", newPwd);
                            application.setKckpPass(newPwdEn);
                            logout();
                            OnDestroy();
                            closeForm();
                        }
                    }
                });
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        mView = null;
        setListener(false);
        DialogUtils.destroyDialog();
    }

    /**
     * 退出登录
     */
    private void logout() {
        // 清空sp的账号，密码，token，发全局消息
        PrefUtils.removeStringData(mContext, "loginInfo");
        PrefUtils.removeStringData(mContext, "loginUserName");
        PrefUtils.removeStringData(mContext, "loginPassword");
        //清除缓存
        application.setLoginInfo(null);
        application.setToken(null);
        //回到登录页
        if (FrameworkManager.getInstance().isPopForm()) {
            FrameworkManager.getInstance().closePopForm();
        }
        FrameworkManager.getInstance().destroyAllForm();
    }
}
